package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.AfterSalesDetailActivity;
import com.hemaapp.yjnh.activity.AfterSalesListActivity;
import com.hemaapp.yjnh.bean.AfterSale;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import xtom.frame.view.XtomListView;

/**
 * 买家的售后列表的适配器
 */
public class AfterSalesAdapter extends BaseAdapter {

    private ArrayList<AfterSale> afterSales;
    private Context mContext;

    public AfterSalesAdapter(Context mContext, ArrayList<AfterSale> afterSales) {
        super(mContext);
        this.mContext = mContext;
        this.afterSales = afterSales;
    }

    @Override
    public int getCount() {
        int size = afterSales == null ? 0 : afterSales.size();
        return size == 0 ? 1 : afterSales.size();
    }

    @Override
    public boolean isEmpty() {
        int size = afterSales == null ? 0 : afterSales.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        ViewHolder holder;
        if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_aftersale, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.TAG_VIEWHOLDER);
        }
        holder.setDatas(position);
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.tv_order_no)
        TextView tvOrderNo;
        @Bind(R.id.tv_state)
        TextView tvState;
        @Bind(R.id.iv_pic)
        ImageView ivPic;
        @Bind(R.id.tv_good_name)
        TextView tvGoodName;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_count)
        TextView tvCount;
        @Bind(R.id.tv_total_fee)
        TextView tvTotalFee;
        @Bind(R.id.tv_freight)
        TextView tvFreight;
        @Bind(R.id.tv_return)
        TextView tvReturn;//退的现金
        @Bind(R.id.tv_score)
        TextView tvScore;//退的粮票
        int position = 0;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent itAfter = new Intent(mContext, AfterSalesDetailActivity.class);
                    itAfter.putExtra("order_id", afterSales.get(position).getId());
                    itAfter.putExtra("isSeller", false);
                    ((AfterSalesListActivity) mContext).startActivityForResult(itAfter, AfterSalesListActivity.REQUEST_MOD);
                }
            });
        }

        public void setDatas(int position) {
            this.position = position;
            AfterSale afterSale = afterSales.get(position);
            ImageUtils.loadSmalImage(mContext, afterSale.getImgurl(), ivPic);
            tvOrderNo.setText("订单号："+afterSale.getBill_sn());
            tvState.setText(getState(afterSale));
            tvGoodName.setText(afterSale.getName());
            tvPrice.setText("¥" + uuUtils.formatAfterDot2(afterSale.getPrice()));
            tvCount.setText("x" + afterSale.getBuycount());

            double price = Double.valueOf(afterSale.getPrice());
            double score = Double.valueOf(afterSale.getScore());
            double count = Double.valueOf(afterSale.getBuycount());
            double freight = Double.valueOf(afterSale.getShipping_fee());
            double totalFee = price * count;
            double totalScore = score * count;
            tvTotalFee.setText("总价：¥" + uuUtils.formatAfterDot2(totalFee + freight+""));
            tvFreight.setText("（含运费¥"+uuUtils.formatAfterDot2(freight+"")+"）");
            tvScore.setText(uuUtils.formatAfterDot2(totalScore+""));
            tvReturn.setText("¥" + (uuUtils.formatAfterDot2(totalFee - totalScore +freight+ "")));
        }
    }

    /**
     * 获取当前的售后服务状态
     *
     * @param afterSale
     * @return
     */
    private String getState(AfterSale afterSale) {
        // 状态	0正常;1申请中;2等待买家发货;3拒绝;4退货中;5退货完成
        String itemtype = afterSale.getItemtype();
        if ("1".equals(itemtype)) {
            return "审核中";
        } else if ("2".equals(itemtype)) {
            return "待退货";
        } else if ("5".equals(itemtype)) {
            return "退款成功";
        } else if ("3".equals(itemtype)) {
            return "退款失败";
        } else {
            return "退货中";
        }
    }
}
